<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>09 倒计时广告</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #box {
            width: 400px;
            height: 200px;
            border: 1px solid #000000;
            /*margin: 0 auto;*/
            position: absolute;
            top: 50%;
            margin-top: -100px;
            left: 50%;
            margin-left: -200px;
        }

        #close {
            position: absolute;
            right: 5px;
            top: 5px;
            cursor: pointer;
        }
    </style>
</head>

<body>
    <h1>09 倒计时广告</h1>
    <div id="box">
        <p id="time">5秒</p>
        <p id="close">X</p>
    </div>
</body>
<script src="../common.js"></script>
<script>
    /*
    
        需求：倒计时广告
            * 开启定时器，让时间发生变化
            * 点击按钮时提前关闭

    */

    (function() {

        // 找节点
        var box = getid('box');
        var time = getid('time');
        var close = getid('close');
        var num = 15;

        // 开启定时器
        var timer = setInterval(function() {
            num--;
            if (num <= 0) {
                // 到达临界点
                clearInterval(timer);
                box.style.display = 'none';
            };

            time.innerHTML = num + '秒';

        },1000);

        // 点击按钮关闭窗口
        close.onclick = function () {
            box.style.display = 'none';
            clearInterval(timer);
        }

    })();

</script>

</html>